package com.bootdo.shop.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.shop.domain.SpecificationDO;
import com.bootdo.shop.service.SpecificationService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 规格表

 * 
 * @author yhg
 * @email 411741962@qq.com
 * @date 2017-12-22 13:43:28
 */
 
@Controller
@RequestMapping("/shop/specification")
public class SpecificationController {
	@Autowired
	private SpecificationService specificationService;
	
	@GetMapping()
	@RequiresPermissions("shop:specification:specification")
	String Specification(){
	    return "shop/specification/specification";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("shop:specification:specification")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<SpecificationDO> specificationList = specificationService.list(query);
		int total = specificationService.count(query);
		PageUtils pageUtils = new PageUtils(specificationList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("shop:specification:add")
	String add(){
	    return "shop/specification/add";
	}

	@GetMapping("/edit/{specificationId}")
	@RequiresPermissions("shop:specification:edit")
	String edit(@PathVariable("specificationId") Long specificationId,Model model){
		SpecificationDO specification = specificationService.get(specificationId);
		model.addAttribute("specification", specification);
	    return "shop/specification/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("shop:specification:add")
	public R save( SpecificationDO specification){
		if(specificationService.save(specification)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("shop:specification:edit")
	public R update( SpecificationDO specification){
		specificationService.update(specification);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("shop:specification:remove")
	public R remove( Long specificationId){
		if(specificationService.remove(specificationId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("shop:specification:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] specificationIds){
		specificationService.batchRemove(specificationIds);
		return R.ok();
	}
	
}
